package com.ligure;

import java.util.Arrays;
import java.util.EmptyStackException;

public class Stack<T> {
    private T[] elements;
    private int size;

    public Stack() {
        this(10);
    }

    public Stack(int size) {
        elements = (T[]) new Object[size];
    }

    public T push(T value) {
        if (size == elements.length) {
            elements = Arrays.copyOf(elements, elements.length * 2);
        }
        elements[size] = value;
        size++;
        return value;
    }

    public T pop() {
        if (empty()) {
            throw new EmptyStackException();
        }
        T value = elements[size - 1];
        elements[size - 1] = null;
        size--;
        return value;
    }

    public T peek() {
        if (empty()) {
            throw new EmptyStackException();
        }
        return elements[size - 1];
    }

    public int size() {
        return size;
    }

    public boolean empty() {
        return size == 0;
    }

}
